package com.tianzehua.netty.protobufexample;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

/**
 * @author tianzehua
 * @date  2019/03/14
 */
public class TestClientHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg)  {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx)  {
        /* 当客户端在处于活动的状态下，向服务器端发出相应的请求 */
       /* MyDataInfo.Person person = MyDataInfo.Person.newBuilder()
                .setName("张三").setAge(20).setAddress("北京").build();
        ctx.channel().writeAndFlush(person);*/
       int random = new Random().nextInt(3);
        System.out.println("随机数为：" + random);
        MyDataInfo.MyMessage myMessage ;
       if(0 == random){
           myMessage = MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.PersonType)
                   .setPerson(MyDataInfo.Person.newBuilder().setName("张三").setAddress("北京").setAge(20)).build();
       }else if (1 == random){
           myMessage = MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.DogType)
                   .setDog(MyDataInfo.Dog.newBuilder().setName("小狗").setAge(2)).build();
       }else {
           myMessage = MyDataInfo.MyMessage.newBuilder().setDataType(MyDataInfo.MyMessage.DataType.CatType)
                   .setCat(MyDataInfo.Cat.newBuilder().setName("小猫").setCity("上海")).build();
       }
        ctx.channel().writeAndFlush(myMessage);
    }
}
